package com.zzedu.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.system.domain.SysPost;
import com.zzedu.system.service.ISysPostService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:post:list')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody SysPost post) {
        IPage<SysPost> list = postService.selectPostList(post.getPageDTO(), post);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@permission.hasAnyPermissions('system:post:export')")
    @RequestMapping("/export")
    public void export(HttpServletResponse response, SysPost post) {
        IPage<SysPost> list = postService.selectPostList(new Page<>(1L, -1L), post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list.getRecords(), "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:post:query')")
    @RequestMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId) {
        return success(postService.getById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysPost post) {
        return toAjax(postService.save(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysPost post) {
        return toAjax(postService.updateById(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{postIds}")
    public AjaxResult remove(@PathVariable List<Long> postIds) {
        return toAjax(postService.removeBatchByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @RequestMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return success(posts);
    }
}
